import request from '@/utils/request'

// 错题本管理
export function listBooks(userId) {
  return request({
    url: '/wrongQuestion/book/list',
    method: 'get',
    params: { userId }
  })
}

export function getDefaultBook(userId) {
  return request({
    url: '/wrongQuestion/book/default',
    method: 'get',
    params: { userId }
  })
}

export function pageBooks(userId, pageNum, pageSize) {
  return request({
    url: '/wrongQuestion/book/page',
    method: 'get',
    params: { userId, pageNum, pageSize }
  })
}

export function createBook(data) {
  return request({
    url: '/wrongQuestion/book',
    method: 'post',
    data
  })
}

export function updateBook(data) {
  return request({
    url: '/wrongQuestion/book',
    method: 'put',
    data
  })
}

export function deleteBook(bookId) {
  return request({
    url: `/wrongQuestion/book/${bookId}`,
    method: 'delete'
  })
}

export function setDefaultBook(userId, bookId) {
  return request({
    url: `/wrongQuestion/book/default/${bookId}`,
    method: 'put',
    params: { userId }
  })
}

// 错题记录管理
export function pageRecords(params) {
  return request({
    url: '/wrongQuestion/record/page',
    method: 'get',
    params: params
  })
}



export function addRecord(bookId, questionAnswerId) {
  return request({
    url: '/wrongQuestion/record',
    method: 'post',
    params: { bookId, questionAnswerId }
  })
}

export function batchAddRecords(bookId, questionAnswerIds) {
  return request({
    url: '/wrongQuestion/record/batch',
    method: 'post',
    params: { bookId },
    data: questionAnswerIds
  })
}

export function removeRecord(recordId) {
  return request({
    url: `/wrongQuestion/record/${recordId}`,
    method: 'delete'
  })
}

export function batchRemoveRecords(recordIds) {
  return request({
    url: '/wrongQuestion/record/batch',
    method: 'delete',
    data: recordIds
  })
}

export function markMastered(recordId, mastered) {
  return request({
    url: `/wrongQuestion/record/master/${recordId}`,
    method: 'put',
    params: { mastered }
  })
}

export function updateRecordTags(recordId, tagsJson) {
  return request({
    url: `/wrongQuestion/record/tags/${recordId}`,
    method: 'put',
    params: { tagsJson }
  })
}

// 错题练习
export function generatePractice(bookId, count, strategy) {
  return request({
    url: '/wrongQuestion/practice/generate',
    method: 'get',
    params: { bookId, count, strategy }
  })
}

export function submitPractice(recordId, answer, timeSpent) {
  return request({
    url: '/wrongQuestion/practice/submit',
    method: 'post',
    params: { recordId, answer, timeSpent }
  })
}

// 错题统计
export function pageStats(userId, bookId, pageNum, pageSize) {
  return request({
    url: '/wrongQuestion/stats/page',
    method: 'get',
    params: { userId, bookId, pageNum, pageSize }
  })
}

export function getOverviewStats(userId, bookId) {
  return request({
    url: '/wrongQuestion/stats/overview',
    method: 'get',
    params: { userId, bookId }
  })
}

export function getKnowledgeStats(userId, bookId) {
  return request({
    url: '/wrongQuestion/stats/knowledge',
    method: 'get',
    params: { userId, bookId }
  })
}

export function getTypeStats(userId, bookId) {
  return request({
    url: '/wrongQuestion/stats/type',
    method: 'get',
    params: { userId, bookId }
  })
}

// 智能推荐
export function recommendQuestions(recordId, count, strategy) {
  return request({
    url: '/wrongQuestion/recommend',
    method: 'get',
    params: { recordId, count, strategy }
  })
}

export function getRecordDetail(recordId) {
  return request({
    url: `/wrongQuestion/record/${recordId}`,
    method: 'get'
  })
}

// 获取可同步的错题
export function getWrongAnswers(userId) {
  return request({
    url: '/wrongQuestion/wrongAnswers',
    method: 'get',
    params: { userId }
  })
}

// 同步错题
export function syncWrongAnswers(bookId, answerIds) {
  return request({
    url: '/wrongQuestion/sync',
    method: 'post',
    params: { bookId },
    data: answerIds
  })
}
